// pages/book-detail/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active:0,
        isDesk:false, //false代表不在书桌
        deskid:'' //记录书桌id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 基本内容渲染
        wx.getStorage({
            key:'detail',
            success:(local)=>{
                // console.log(local);
                this.setData(local.data)
                // 获取书桌状态
                this.initDeskState(local.data.objectId)
                // 获取目录
                let uuid = local.data.sourceUrl.split('/source/')[1]
                wx.request({
                  url: `https://m.yuedu.163.com/reader/book/info.json?source_uuid=${uuid}&catalog_only=true`,
                  success:(res)=>{
                    // console.log(res);
                    let {catalog} = res.data.data
                    this.setData({catalog})
                  }
                })
            }
        })
        // 目录渲染
        // https://m.yuedu.163.com/reader/book/info.json?source_uuid=a2a4841d50e04a31a6185becd23ff999_4&catalog_only=true
    },
    initDeskState(bookid){  // 获取书桌状态
        //获取用户id
        let {userInfo} = getApp().globalData
        if(userInfo){
            let {userid} = userInfo
            let url = `classes/MiniDesk?where={"userid":"${userid}","bookid":"${bookid}"}`
            getApp().fly.get(url).then(res=>{
                // console.log(res);
                let {results} = res.data
                if(results.length){
                    this.setData({
                        isDesk:true,
                        deskid:results[0].objectId //记录书桌id
                    })
                }
            })
        }
    },
    onChange(ev){
        console.log(ev);
        this.setData({
            active:ev.detail.index
        })
    },
    handleAdd(){ //加入书桌
        let {userInfo} = getApp().globalData
        if(!userInfo){
            wx.showModal({
                title:'授权登录',
                content:'点击确认可以一键授权登录',
                success:async({confirm})=>{
                    console.log(confirm);
                    if(confirm){
                        await getApp().handleQuickLogin() //必须要用同步语法
                        this.initDeskState(this.data.objectId) //登录成功后才能尝试改变书桌状态
                    }
                }
            })
            return
        }
        // 加入书桌
        let {userid} = userInfo
        let bookid = this.data.objectId
        let {iconUrl,title} = this.data
        getApp().fly.post('classes/MiniDesk',{userid,bookid,iconUrl,title}).then(res=>{
            console.log(res);
            this.setData({
                isDesk:true,
                deskid:res.data.objectId  //记录书桌id
            })
            wx.showToast({
              title: '加入成功',
              icon:'none'
            })
        })
    },
    handleDel(){    //移出书桌
        let {deskid} = this.data
        getApp().fly.delete(`classes/MiniDesk/${deskid}`).then(res=>{
            this.setData({
                isDesk:false
            })
            wx.showToast({
              title: '移出成功',
              icon:'none'
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})